<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    use HasFactory;
    protected $table="zfm_comments";
    protected $guarded=[];
    //添加一条顶置评论
    public  static  function  topComments($params = array()){
        return self::create($params);
    }
    //修改&&删除当前评论信息
    public static function updateComments(int $id,string $comment=NULL){
        return   empty($comment)?
            self::where('id',$id)
                ->update(['status'=>'2']):
            self::where('id',$id)
                ->update(['comments' => $comment]);
    }
    //查询所有评论
    static function AllComments(){
        return self::all()->toArray();
    }
    //查看当前评论
    public  static  function  lookComments(int $id,int $page=5){
        return $id==0?
            self::where('parent_id',$id)
                ->latest()
                ->limit(5)
                ->select(['id','name','parent_id','comment','created_at'])
                ->get()
                ->toArray()
            :self::where('parent_id',$id)
                ->select(['id','name','parent_id','comment','created_at'])
                ->get()
                ->toArray();
    }
    //查询上级评论的状态是否正常
    public static function commentStatus(int $parentId){
        $comment= self::where('id', $parentId)
            ->where('status', '1')
            ->first();
        return $comment?$comment->toArray():Null;
    }


}
